package com.android.example.app3;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ImageGalleryActivity extends AppCompatActivity implements ItemRemovedListener {
    // 定义请求权限的常量
    // RecyclerView组件的引用
    private RecyclerView recyclerView;
    private TextView textView;
    private CursorImageAdapter adapter;

    private String key_string;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery); // 设置活动的布局资源

        // 初始化RecyclerView组件
        recyclerView = findViewById(R.id.recyclerView);
        textView = findViewById(R.id.textView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

        //搜索包含关键词图片
        key_string = getIntent().getStringExtra("key_string");

        SearchImages(key_string);
    }

    @SuppressLint("SetTextI18n")
    private void SearchImages(String key_string) {
        String string = key_string + "_";
        // 检查传入的搜索关键字是否为空
        if (key_string == null || key_string.trim().isEmpty()) {
            // 当没有搜索到相关结果时，显示消息
            textView.setText("请注意：搜索关键字为空，\n请输入日期或包含单号的任意号码，\n如日期：20240101...，\n如搜索单号建议4位尾数以上。");
            textView.setVisibility(View.VISIBLE);
            return;
        }

        Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
        String selection = MediaStore.Images.Media.DISPLAY_NAME + " LIKE ? ESCAPE '/' ";
        String[] selectionArgs = new String[]{"%" + string.replace("_", "/_") + "%"};

        Cursor cursor = getContentResolver().query(
                collection,
                null,
                selection,
                selectionArgs,
                MediaStore.Images.Media.DATE_ADDED + " DESC"
        );

        if (cursor != null) {
            if (cursor.moveToFirst()) { // 确保Cursor至少有一行数据
                textView.setVisibility(View.GONE);
                adapter = new CursorImageAdapter(this, cursor);
                adapter.setItemRemovedListener(this);
                recyclerView.setAdapter(adapter);
            } else {
                // 当没有搜索到相关结果时，显示消息
                textView.setText(key_string + "\n没有搜索到相关结果");
                textView.setVisibility(View.VISIBLE);
            }
        } else {
            // 当cursor为null时，显示Toast消息
            Toast.makeText(this, key_string + "\n查询失败", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * @param fileName
     */
    @Override
    public void onItemRemoved(String fileName) {

    }
}
